﻿using System;
using System.Text;
using System.Web.Mvc;
using MvcApplication7.Models;

namespace MvcApplication7.Controllers
{
    public class Test1FolderController : Controller
    {
        public ActionResult Index()
        {
            var viewName = "ViewUserControl1";
            if (String.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            var builder = new StringBuilder();
            var innerBuilder = new StringBuilder();

            ViewData.Model = new ViewUserControl1Model(
                action =>
                    {
                        var oldBuilder = builder;
                        builder = innerBuilder;
                        action();
                        builder = oldBuilder;
                    }
                );

            using (var writer = new MyStringWriter(() => builder))
            {
                var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                viewEngineResult.View.Render(
                    new ViewContext(
                        ControllerContext,
                        viewEngineResult.View,
                        ViewData,
                        TempData,
                        writer),
                    writer);
            }
            return View(new Test1FolderModel(innerBuilder.ToString()));
        }
    }
}
